
let designW = 2432;  //设计稿宽
let font_rate = 100;
let htmlFS = 0 ;

const setHtmlSize = ()=>{
    htmlFS = document.body.offsetWidth / designW * font_rate ;
    //适配
    document.getElementsByTagName("html")[0].style.fontSize = `${htmlFS}px`;
    window.fontSize = htmlFS;

    //监测窗口大小变化
    window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", function() {
            htmlFS = document.body.offsetWidth / designW * font_rate ;
            document.getElementsByTagName("html")[0].style.fontSize = `${htmlFS}px`;
        }, false);
}

//设计稿的大小转为实时的rem
const pxToRem=(num)=>{
    return pxToPx(num)+"px";
    return num/font_rate+"rem" ;
}

const pxToVh=(num)=>{
    return num/10.24 + "vh";
}

//设计稿的大小转为实时的px
const pxToPx=(num)=>{
    if(htmlFS==0){
        htmlFS = document.body.offsetWidth / designW * font_rate ;
    }
    //不四舍五入
    return Math.floor(num/font_rate * htmlFS * 100) / 100 ;
}

const transformUnit=(data)=> {
    let unit = window.om.unit;
    let viewWidth = window.om.viewWidth ;
    let viewHeight = window.om.viewHeight ;
    if (unit == "px") {//固定大小的设计
      if (data.indexOf("vh") != -1) {
        return parseFloat(data) * viewHeight + "px";
      } else if (data.indexOf("vw") != -1) {
        return parseFloat(data) * viewWidth + "px";
      } else {
        return data;
      }
    } else {//自适应的设计
      return data;
    }
  }
export {setHtmlSize,pxToRem, pxToPx,pxToVh,transformUnit}

